﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;

namespace WebComponents.UI.BasePage
{

    public class IRegisterControlToJSEnviroment
    {
        public string JsName { get; set; }
        public string JsValue { get; set; }
    }

    public class RegisterControlToJSEnviroment : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            RegisterJs();

        }

        public void RegisterJs()
        {
            List<IRegisterControlToJSEnviroment> JsObj = new List<IRegisterControlToJSEnviroment>();
            StringBuilder sb = new StringBuilder();
            string JsGetControl = String.Format(@"function Ctrl(f) {0}var c = document.getElementById(f);if (c == null || c == 'undefined') return null;else return c;{1}", "{", "}");
            sb.AppendLine(JsGetControl);
            ApplyToControls(Page, (ControlDelegate)delegate(Control control)
            {
                if (!String.IsNullOrEmpty(control.ID))
                {
                    JsObj.Add(new IRegisterControlToJSEnviroment { JsName = String.Format("_{0}", control.ID), JsValue = control.ClientID });
                }
            });
            var js = from t in JsObj
                     //orderby t.JsName
                     select t;
            //
            //should be base on page load only!
            //
            string previousVar = "";
            foreach (var o in js)
            {
                if (previousVar != o.JsName)
                {
                    sb.Append(String.Format("var {0}='{1}';", o.JsName, o.JsValue));
                    previousVar = o.JsName;
                }
            }
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "RegisterControlToJSEnviroment", sb.ToString(), true);
        }

        public delegate void ControlDelegate(Control control);
        public static void ApplyToControls(Control root, ControlDelegate apply)
        {
            if (root.HasControls())
            {
                foreach (Control control in root.Controls)
                {
                    apply.Invoke(control);
                    ApplyToControls(control, apply);
                }
            }
        }

    }

    public class RegisterClientCallBackToJSEnviroment : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            RegisterJs();

        }

        public void RegisterJs()
        {
            List<IRegisterControlToJSEnviroment> JsObj = new List<IRegisterControlToJSEnviroment>();
            StringBuilder sb = new StringBuilder();
            string JsGetControl = String.Format(@"function Ctrl(f) {0}var c = document.getElementById(f);if (c == null || c == 'undefined') return null;else return c;{1}", "{", "}");
            sb.AppendLine(JsGetControl);
            RegisterControlToJSEnviroment.ApplyToControls(Page, (RegisterControlToJSEnviroment.ControlDelegate)delegate(Control control)
            {
                if (!String.IsNullOrEmpty(control.ID))
                {
                    if(control.ClientID.IndexOf("ClientCallBack") != -1)
                        JsObj.Add(new IRegisterControlToJSEnviroment { JsName = String.Format("_{0}", control.ID), JsValue = control.ClientID });
                }
            });
            var js = from t in JsObj
                     //orderby t.JsName
                     select t;
            //
            //should be base on page load only!
            //
            string previousVar = "";
            foreach (var o in js)
            {
                if (previousVar != o.JsName)
                {
                    sb.Append(String.Format("var {0}='{1}';", o.JsName, o.JsValue));
                    previousVar = o.JsName;
                }
            }
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "RegisterClientCallBackToJSEnviroment", sb.ToString(), true);
        }


        

    }

}